﻿using Microsoft.IdentityModel.S2S.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IdentityModel.Tokens;
using AG_Apps_Cloud_Services_WCF_Web_Role;

namespace OAuthTokenExplorerWeb
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                //Get ContextToken from Form Variable spAppToken
                string spAppToken = TokenHelper.GetContextTokenFromRequest(HttpContext.Current.Request);
                litContextTokenValue.Text = spAppToken;

                SharePointContextToken spContextToken = ReadToken(spAppToken);

                litContextToken.Text = spContextToken.ToString().Replace(",", ",<br/>").Replace("}.{", "<br/>}<br/>.<br/>{").Replace("{", "{<br/>");

                string hostweb = Page.Request["SPHostUrl"];
                Uri hostUri = new Uri(hostweb);

                string accessToken = TokenHelper.GetAccessToken(spContextToken, hostUri.Authority).AccessToken;
                litAccessTokenValue.Text = accessToken;

                JwtSecurityTokenHandler tokenhandler = new JwtSecurityTokenHandler();
                SecurityToken st = tokenhandler.ReadToken(accessToken);

                litAccessToken.Text = st.ToString().Replace(",", ",<br/>").Replace(",", ",<br/>").Replace("}.{", "<br/>}<br/>.<br/>{").Replace("{", "{<br/>");


                string accessTokenAppOnly = TokenHelper.GetAppOnlyAccessToken(spContextToken.TargetPrincipalName,
                                                           hostUri.Authority, spContextToken.Realm).AccessToken;
                litAccessTokenValueAppOnly.Text = accessTokenAppOnly;

                JwtSecurityTokenHandler tokenhandlerAppOnly = new JwtSecurityTokenHandler();
                SecurityToken stAppOnly = tokenhandler.ReadToken(accessTokenAppOnly);

                litAccessTokenAppOnly.Text = stAppOnly.ToString().Replace(",", ",<br/>").Replace(",", ",<br/>").Replace("}.{", "<br/>}<br/>.<br/>{").Replace("{", "{<br/>");



                litClientId.Text = TokenHelper.ClientId;





                using (var clientContext = TokenHelper.GetClientContextWithAccessToken(hostweb, accessToken))
                {
                    var web = clientContext.Web;
                    var user = clientContext.Web.CurrentUser;

                    clientContext.Load(web);
                    clientContext.Load(user);
                    clientContext.ExecuteQuery();

                    litHostWeb.Text = hostweb + " (" + web.Title + ")";
                    litUser.Text = "LoginName = " + user.LoginName + "<br/>" +
                                    "NameId = " + user.UserId.NameId + "<br/>" +
                                    "NameIdUssuer = " + user.UserId.NameIdIssuer + "<br/>" +
                                    "PrincipalType = " + user.PrincipalType.ToString();
                }

            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }

        }

        private SharePointContextToken ReadToken(string spAppToken)
        {
            //TokenHelper.CreateJsonWebSecurityTokenHandler changed from private to public!!
            JsonWebSecurityTokenHandler tokenHandler = TokenHelper.CreateJsonWebSecurityTokenHandler();
            SecurityToken securityToken = tokenHandler.ReadToken(spAppToken);
            JsonWebSecurityToken jwtToken = securityToken as JsonWebSecurityToken;
            SharePointContextToken contextToken = SharePointContextToken.Create(jwtToken);

            return contextToken;

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }
    }
}